<template>
  <div class="pugtablebox">
    <div class="mb-3 flex justify-between">
      <div>
        <el-button size="small" type="success" @click="handleAdd" :icon="CirclePlus">新增</el-button>
        <el-button size="small" type="danger" :icon="Delete">批量删除</el-button>
      </div>
      <div>
        <el-input v-model="keyword" class="w-50 m-2" size="small" placeholder="请输入搜索关键词" :suffix-icon="Search" />
        <el-button size="small" type="success" :icon="Search">搜索</el-button>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="日期" width="380">
        <template #default="scope">
          <div class="border border-dashed py-2 px-4 rounded" :class="[scope.row.status === 1 ? 'active' : 'inactive']"
            data-v-71eeddcb="">
            <h5 class="text-md font-bold" data-v-71eeddcb="">满100减1</h5><small data-v-71eeddcb="">2022-05-10 00:00:00 ~
              2022-05-14 00:00:00</small>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="日期">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon>
              <timer />
            </el-icon>
            <span style="margin-left: 10px">{{ scope.row.date }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="名字">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>name: {{ scope.row.name }}</div>
              <div>address: {{ scope.row.address }}</div>
            </template>
            <template #reference>
              <el-tag>{{ scope.row.name }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          {{ scope.row.status }}
          <el-switch v-model="scope.row.status" class="ml-2" width="60px" inline-prompt :active-value="1"
            :inactive-value="0" active-text="发布" inactive-text="不发布" />
        </template>
      </el-table-column>
      <el-table-column label="Operations">
        <template #default="scope">
          <el-button size="small" :icon="Edit" @click="handleEdit(scope.$index, scope.row)">Edit</el-button>
          <el-button size="small" type="danger" :icon="Delete" @click="handleDelete(scope.$index, scope.row)">Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="justify-center mt-5" background layout="prev, pager, next" :total="1000" />
  </div>

  <pug-drawer ref="couponAddRef" title="新增优惠券" @submit="handleSubmit">
    <el-form :model="coupon" ref="couponFormRef" :rules="rules" label-width="80px" :inline="false" size="normal">
      <el-form-item label="名称" prop="title">
        <el-input v-model="coupon.title" placeholder="优惠券名称"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="coupon.type" border>
          <el-radio :label="0">满减</el-radio>
          <el-radio :label="1">折扣</el-radio>
        </el-radio-group>
      </el-form-item>
       <el-form-item label="面值" prop="price">
        <el-input v-model="coupon.price" placeholder="面值">
          <template #append>{{coupon.type?"折":"元"}}</template>
        </el-input>
      </el-form-item>
      <el-form-item label="发行量" prop="total">
        <el-input-number v-model="coupon.total" placeholder="发行量" :min="0" :max="10000"></el-input-number>
      </el-form-item>
       <el-form-item label="次数" prop="num">
        <el-input-number v-model="coupon.num" placeholder="次数" :min="0" :max="10000"></el-input-number>
      </el-form-item>
       <el-form-item label="最低价格" prop="minprice">
        <el-input v-model="coupon.minprice" placeholder="最低价格"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
          <el-input-number v-model="coupon.sort" placeholder="排序" :min="0" :max="10000"></el-input-number>
      </el-form-item>
       <el-form-item label="活动时间" prop="starttime">
        <el-input v-model="coupon.starttime"></el-input>
        <el-input v-model="coupon.endtime" ></el-input>
        <el-date-picker 
          v-model="timerange" 
          type="datetimerange" 
          value-format="YYYY-MM-DD HH:mm:ss"
          range-separator="To"  
          start-placeholder="选择开始时间"
          end-placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发布状态" prop="content">
         <el-switch v-model="coupon.status" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
       <el-form-item label="描述" prop="desc">
        <el-input v-model="coupon.desc" placeholder="描述" type="textarea" :rows="5"></el-input>
      </el-form-item>
    </el-form>
  </pug-drawer>
</template>

<script setup>
import { Timer } from '@element-plus/icons-vue'
import { ref, reactive,watch } from 'vue'
import { CirclePlus, Top, Bottom, Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'

const keyword = ref("");


const handleEdit = (index, row) => {
  console.log(index, row)
}
const handleDelete = (index, row) => {
  console.log(index, row)
}

const tableData = [
  {
    date: '2016-05-03',
    name: 'Tom',
    status: 1,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    status: 0,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    status: 1,
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    status: 0,
    address: 'No. 189, Grove St, Los Angeles',
  },
];



const timerange = ref([]);
watch(timerange,(val)=>{
    coupon.starttime = val[0];
    coupon.endtime = val[1];
})

const couponAddRef = ref(null);
const couponFormRef = ref(null);
const rules =  {
    title: [{
      required: true,
      message: '优惠券标题不能为空',
      trigger: 'blur'
    }],
    content: [{
      required: true,
      message: '优惠券内容不能为空',
      trigger: 'blur'
    }]
  };
const coupon = reactive({
  title: "",
  starttime:"",
  endtime:"",
  content: ""
})

const handleAdd = () => {
  couponAddRef.value.open();
}

const handleSubmit = () => {
  couponFormRef.value.validate((valid) => {
      if(!valid){
        return;
      }
      couponAddRef.value.showLoading();
      alert("保存数据" + JSON.stringify(coupon))
  })
}

</script>

<style>
.active {
  --tw-bg-opacity: 1;
  background-color: rgba(255, 241, 242, var(--tw-bg-opacity));
  --tw-border-opacity: 1;
  border-color: rgba(254, 205, 211, var(--tw-border-opacity));
  --tw-text-opacity: 1;
  color: rgba(248, 113, 113, var(--tw-text-opacity));
}
</style>